namespace Server.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class InitialCreate : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.Batteries",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Serial = c.String(nullable: false),
                        CarId = c.Int(),
                        BatteryType_Id = c.Int(nullable: false),
                        Station_Id = c.Int(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Cars", t => t.CarId)
                .ForeignKey("dbo.BatteryTypes", t => t.BatteryType_Id, cascadeDelete: true)
                .ForeignKey("dbo.Stations", t => t.Station_Id)
                .Index(t => t.CarId)
                .Index(t => t.BatteryType_Id)
                .Index(t => t.Station_Id);

            CreateTable(
                "dbo.BatteryTypes",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Name = c.String(nullable: false),
                        Description = c.String(),
                    })
                .PrimaryKey(t => t.Id);

            CreateTable(
                "dbo.CarModels",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Range = c.Double(nullable: false),
                        Description = c.String(),
                        BatteryType_Id = c.Int(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.BatteryTypes", t => t.BatteryType_Id)
                .Index(t => t.BatteryType_Id);

            CreateTable(
                "dbo.Cars",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        RegistrationNo = c.String(),
                        CarModel_Id = c.Int(nullable: false),
                        User_Id = c.Int(nullable: false),
                        BatteryId = c.Int(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.CarModels", t => t.CarModel_Id, cascadeDelete: true)
                .ForeignKey("dbo.Users", t => t.User_Id, cascadeDelete: true)
                .ForeignKey("dbo.Batteries", t => t.BatteryId)
                .Index(t => t.CarModel_Id)
                .Index(t => t.User_Id)
                .Index(t => t.BatteryId);

            CreateTable(
                "dbo.Reservations",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Date = c.DateTime(nullable: false),
                        Car_Id = c.Int(nullable: false),
                        User_Id = c.Int(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Cars", t => t.Car_Id, cascadeDelete: true)
                .ForeignKey("dbo.Users", t => t.User_Id)
                .Index(t => t.Car_Id)
                .Index(t => t.User_Id);

            CreateTable(
                "dbo.PartReservations",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Date = c.DateTime(nullable: false),
                        Battery_Id = c.Int(),
                        BatteryType_Id = c.Int(nullable: false),
                        Reservation_Id = c.Int(nullable: false),
                        Station_Id = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Batteries", t => t.Battery_Id)
                .ForeignKey("dbo.BatteryTypes", t => t.BatteryType_Id, cascadeDelete: true)
                .ForeignKey("dbo.Reservations", t => t.Reservation_Id, cascadeDelete: true)
                .ForeignKey("dbo.Stations", t => t.Station_Id, cascadeDelete: true)
                .Index(t => t.Battery_Id)
                .Index(t => t.BatteryType_Id)
                .Index(t => t.Reservation_Id)
                .Index(t => t.Station_Id);

            CreateTable(
                "dbo.Stations",
                c => new
                    {
                        Id = c.Int(nullable: false),
                        Name = c.String(nullable: false),
                        StreetAddress = c.String(nullable: false),
                        ZipCode = c.String(nullable: false),
                        City = c.String(nullable: false),
                        Longitude = c.Double(nullable: false),
                        Latitude = c.Double(nullable: false),
                    })
                .PrimaryKey(t => t.Id);

            CreateTable(
                "dbo.Routes",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Time = c.Int(nullable: false),
                        Distance = c.Double(nullable: false),
                        From_Id = c.Int(),
                        To_Id = c.Int(),
                        Station_Id = c.Int(),
                        Station_Id1 = c.Int(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Stations", t => t.From_Id)
                .ForeignKey("dbo.Stations", t => t.To_Id)
                .ForeignKey("dbo.Stations", t => t.Station_Id)
                .ForeignKey("dbo.Stations", t => t.Station_Id1)
                .Index(t => t.From_Id)
                .Index(t => t.To_Id)
                .Index(t => t.Station_Id)
                .Index(t => t.Station_Id1);

            CreateTable(
                "dbo.Users",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Name = c.String(nullable: false),
                        AddressLine1 = c.String(nullable: false),
                        AddressLine2 = c.String(),
                        Phone = c.String(nullable: false),
                        Email = c.String(nullable: false),
                        Password = c.String(nullable: false),
                        Admin = c.Boolean(nullable: false),
                        ZipCodeCity_ZipCode = c.Int(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.ZipCodeCities", t => t.ZipCodeCity_ZipCode)
                .Index(t => t.ZipCodeCity_ZipCode);

            CreateTable(
                "dbo.ZipCodeCities",
                c => new
                    {
                        ZipCode = c.Int(nullable: false),
                        City = c.String(nullable: false),
                    })
                .PrimaryKey(t => t.ZipCode);
            
        }
        
        public override void Down()
        {
            DropForeignKey("dbo.Batteries", "Station_Id", "dbo.Stations");
            DropForeignKey("dbo.Cars", "BatteryId", "dbo.Batteries");
            DropForeignKey("dbo.Batteries", "BatteryType_Id", "dbo.BatteryTypes");
            DropForeignKey("dbo.Cars", "User_Id", "dbo.Users");
            DropForeignKey("dbo.Users", "ZipCodeCity_ZipCode", "dbo.ZipCodeCities");
            DropForeignKey("dbo.Reservations", "User_Id", "dbo.Users");
            DropForeignKey("dbo.PartReservations", "Station_Id", "dbo.Stations");
            DropForeignKey("dbo.Routes", "Station_Id1", "dbo.Stations");
            DropForeignKey("dbo.Routes", "Station_Id", "dbo.Stations");
            DropForeignKey("dbo.Routes", "To_Id", "dbo.Stations");
            DropForeignKey("dbo.Routes", "From_Id", "dbo.Stations");
            DropForeignKey("dbo.PartReservations", "Reservation_Id", "dbo.Reservations");
            DropForeignKey("dbo.PartReservations", "BatteryType_Id", "dbo.BatteryTypes");
            DropForeignKey("dbo.PartReservations", "Battery_Id", "dbo.Batteries");
            DropForeignKey("dbo.Reservations", "Car_Id", "dbo.Cars");
            DropForeignKey("dbo.Cars", "CarModel_Id", "dbo.CarModels");
            DropForeignKey("dbo.Batteries", "CarId", "dbo.Cars");
            DropForeignKey("dbo.CarModels", "BatteryType_Id", "dbo.BatteryTypes");
            DropIndex("dbo.Users", new[] { "ZipCodeCity_ZipCode" });
            DropIndex("dbo.Routes", new[] { "Station_Id1" });
            DropIndex("dbo.Routes", new[] { "Station_Id" });
            DropIndex("dbo.Routes", new[] { "To_Id" });
            DropIndex("dbo.Routes", new[] { "From_Id" });
            DropIndex("dbo.PartReservations", new[] { "Station_Id" });
            DropIndex("dbo.PartReservations", new[] { "Reservation_Id" });
            DropIndex("dbo.PartReservations", new[] { "BatteryType_Id" });
            DropIndex("dbo.PartReservations", new[] { "Battery_Id" });
            DropIndex("dbo.Reservations", new[] { "User_Id" });
            DropIndex("dbo.Reservations", new[] { "Car_Id" });
            DropIndex("dbo.Cars", new[] { "BatteryId" });
            DropIndex("dbo.Cars", new[] { "User_Id" });
            DropIndex("dbo.Cars", new[] { "CarModel_Id" });
            DropIndex("dbo.CarModels", new[] { "BatteryType_Id" });
            DropIndex("dbo.Batteries", new[] { "Station_Id" });
            DropIndex("dbo.Batteries", new[] { "BatteryType_Id" });
            DropIndex("dbo.Batteries", new[] { "CarId" });
            DropTable("dbo.ZipCodeCities");
            DropTable("dbo.Users");
            DropTable("dbo.Routes");
            DropTable("dbo.Stations");
            DropTable("dbo.PartReservations");
            DropTable("dbo.Reservations");
            DropTable("dbo.Cars");
            DropTable("dbo.CarModels");
            DropTable("dbo.BatteryTypes");
            DropTable("dbo.Batteries");
        }
    }
}
